package com.itheima.student.system.entry;

import com.itheima.student.system.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/*
程序的入口
 */
public class Systementry {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    // System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findStudents(list);
                    break;
                case "5":
                    System.out.println("谢谢您的使用");
                    break loop;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    // 增加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号");

        String sid = null;
        while (true) {
            sid = sc.next();
            //boolean flag = isExist(sid, list);
            int index = getIndex(sid, list);
            if (index >= 0) {
                System.out.println("学号已存在,请重新输入:");

            } else {
                System.out.println("系统没有该学号,可以添加:");
                break;
            }
        }

        System.out.println("请输入学生名字");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        // 封装学生对象
        Student student = new Student(sid, name, age, birthday);
        // 添加到集合中
        list.add(student);
        // 给出友好提示
        System.out.println("添加成功!");
    }

    // 删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学号!");
        String sid = sc.next();
        int index = getIndex(sid, list);
        if (index >= 0) {
            list.remove(index);
            System.out.println("删除成功!");
        } else {
            System.out.println("系统没有此学生!");
        }
    }

    //  修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学号:");
        String sid = sc.next();
        int index = getIndex(sid, list);
        if (index >= 0) {
            System.out.println("请输入学生名字");
            String name = sc.next();
            System.out.println("请输入学生年龄");
            int age = sc.nextInt();
            System.out.println("请输入学生生日");
            String birthday = sc.next();
            // 封装学生对象
            Student student = new Student(sid, name, age, birthday);
            // 添加到集合中
            list.set(index, student);
            // 给出友好提示
            System.out.println("修改成功!");
        } else {
            System.out.println("系统没有此学生,请添加!");
        }

    }

    // 查看学生
    public static void findStudents(ArrayList<Student> list) {
        // 判断有没有学生
        if (list.size() == 0) {
            System.out.println("没有学生,请先添加学生信息");
        }
        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "生日");
        for (int i = 0; i < list.size(); i++) {
            // list.get(i)
            Student student = list.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "岁\t" + student.getBirthday());
        }
    }

    // 判断学号是否存在
    //  参数  学号sid(输入的学号)  集合 list
    //  返回值  boolean
    public static boolean isExist(String sid, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            //   list.get(i)
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                return true;
            }
        }
        return false;
    }

    // 判断学号的位置
    //  参数  int(sid)      集合
    //  返回值  index
    public static int getIndex(String sid, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            //  list.get(i)
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }
}
